package sz.szsmk.citizencard.ui.adapter;

import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.bumptech.glide.Glide;
import com.seapeak.recyclebundle.BaseHolder;
import com.seapeak.recyclebundle.BaseRecyclerAdapter;

import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.List;

import sz.szsmk.citizencard.DaggerApplication;
import sz.szsmk.citizencard.R;
import sz.szsmk.citizencard.databinding.CollectionItemBinding;
import sz.szsmk.citizencard.model.network.bean.response.CollecQueryRes;
import sz.szsmk.citizencard.ui.activity.IMainActivity;
import sz.szsmk.citizencard.util.GlideRoundTransform;
import sz.szsmk.citizencard.util.JumpUtils;
import sz.szsmk.citizencard.util.StringUtils;

/**
 * Copyright (c)2018
 * author: zdh
 * created on: 2018/7/13 18:13
 * description:
 */
public class CollecAdapter extends BaseRecyclerAdapter<CollecAdapter.CollecHoler> {

    private Context mContext;
    private List<CollecQueryRes.DataBean> mData = new ArrayList<>();

    public CollecAdapter(Context context) {
        mContext = context;
    }
//
//    @Override
//    public int getItemViewType(int position) {
//        return R.layout.collection_item;
//    }

    @Override
    public CollecHoler onCreateViewHolder(ViewGroup parent, int viewType) {
        return CollecHoler.creat(parent);
    }

    @Override
    public void onBindViewHolder(CollecHoler holder, int position) {
        loadItemData(position, holder.binding);
    }

    private void loadItemData(int position, CollectionItemBinding binding) {
        if (mData != null) {
            CollecQueryRes.DataBean dataBean = mData.get(position);
            binding.userCollAdress.setText(dataBean.getAddress());
            binding.userCollShopname.setText(dataBean.getShopName());
            final double latitude = dataBean.getLatitude();
            final double longitude = dataBean.getLongitude();
            String distanceString = "";

            String beanDistance = dataBean.getDistance();
            double toDouble = StringUtils.convertToDouble(beanDistance, 0)/1000;
            String doubleToString = StringUtils.doubleToString(toDouble);
            binding.userCollDistance.setText("距您" + doubleToString + "km");
            final int areaId = dataBean.getServiceOfficeId();
//            Glide.with(mContext)
//                    .load(dataBean.getPicUrl())
//                    .centerCrop()
//                    .override(100, 100)
//                    .transform(new GlideRoundTransform(mContext))
//                    .into(binding.sev);

            Glide.with(mContext)
                    .load(dataBean.getPicUrl())
                    .error(R.drawable.zhanweitu)
                    .placeholder(R.drawable.zhanweitu)
                    .centerCrop()
                    .override(100, 100)
                    .transform(new GlideRoundTransform(mContext))
                    .into(binding.sev);

            binding.allTypeRl.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {

                    Bundle bundle = new Bundle();
                    bundle.putInt("id", areaId);
                    bundle.putString("type","coll");
                    bundle.putDouble("latitude", latitude);
                    bundle.putDouble("longitude", longitude);
                    JumpUtils.jumptoNatvie(mContext, "netdetail", 0, bundle);
                }
            });
        }
    }

    @Override
    public int getItemCount() {
        return null == mData ? 0 : mData.size();
    }

    public void setListData(List<CollecQueryRes.DataBean> data) {
        mData = data;
    }

    public static class CollecHoler extends BaseHolder {

        private CollectionItemBinding binding;

        public static CollecHoler creat(ViewGroup parent) {
            CollectionItemBinding binding = CollectionItemBinding
                    .inflate(LayoutInflater.from(parent.getContext()), parent, false);
            return new CollecHoler(binding);
        }

        public CollecHoler(CollectionItemBinding binding) {
            super(binding.getRoot());
            this.binding = binding;
        }
    }

}
